package com.dragon.mmochat.model.mergeServer;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.MmochatMain;
import com.dragon.mmochat.model.deal.MmochatBigMoneyDeal;
import com.dragon.mmochat.model.enumType.MmochatBigMoneyDealState;
import com.dragon.mmochat.model.enumType.MmochatPackageGridType;
import com.dragon.mmochat.model.enumType.MmochatPetStateType;
import com.dragon.mmochat.model.gang.MmochatGang;
import com.dragon.mmochat.model.gang.MmochatGangMember;
import com.dragon.mmochat.model.object.MmochatCommonObject;
import com.dragon.mmochat.model.object.MmochatPackageGrid;
import com.dragon.mmochat.model.role.MmochatPet;
import com.dragon.mmochat.service.MmochatBigMoneyDealService;
import com.dragon.mmochat.service.MmochatDao;
import com.dragon.mmochat.service.MmochatGangService;
import com.dragon.mmochat.util.MmochatUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

//合服功能:A服合入到B服，则此程序在A服中执行
public class MergeServer {
	public static int step1_totalCount = 0;
	public static int step1_finishCount = 0;
	public static int step1_subTotal = 0;
	public static int step1_subFinish = 0;
	// 借过东西的人的roleId-->roleId
	public static Map<Integer, Integer> borrowRoleIdMap = new ConcurrentHashMap<Integer, Integer>();
	// 借出东西的人的roleId-->归还给他的东西列表
	public static Map<Integer, List<Object>> ownerMap = new ConcurrentHashMap<Integer, List<Object>>();

	public static int step2_totalCount = 0;
	public static int step2_finishCount = 0;
	public static int step2_trueCount = 0;
	public static final int roleIdAddition = 1000000;
	public static Object lock = new Object();
	// 测试开关
	public static boolean test = false;

	public static void show(String msg) {
		System.out.println(msg);
	}

	public void run() {
		if (!test) {
			if (MmochatMain.mergeServerStep == 1) {
				// step1：bigmoney_deal,元宝订单通过程序进行完结
				step1();

				long startTick = System.currentTimeMillis();
				while (true) {
					if (step1_finishCount < step1_totalCount) {
						int sec = (int) Math
								.max(
										1,
										(System.currentTimeMillis() - startTick) / 1000);
						int speed = Math.max(1, step1_finishCount / sec);
						int restTime = (step1_totalCount - step1_finishCount)
								/ speed;
						int totalTime = sec + restTime;
						show("finish:" + step1_finishCount + "/"
								+ step1_totalCount + ",speed:" + speed
								+ ",restTime:" + restTime + "秒,totalTime:"
								+ totalTime + "秒");
					} else {
						show("finish:" + step1_finishCount + "/"
								+ step1_totalCount);
						break;
					}
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				show("borrowRoleIdMap size:" + borrowRoleIdMap.size());
				{
					// 启动11条线程平摊所有记录进行多线程处理: step1_totalCount = 20 * interval +
					// rest;
					// 前10条各处理interval条记录，第11条线程处理rest条记录
					List<Integer> list = new ArrayList<Integer>(borrowRoleIdMap
							.values());
					int count = list.size();
					int threadNum = 10;
					int start, end;
					step1_subTotal = count;
					step1_subFinish = 0;
					if (count > 0) {
						int interval = Math.max(1, count / threadNum);
						for (int i = 0; i < threadNum; i++) {
							start = i * interval;
							if (start >= count) {
								break;
							}
							end = Math.min(start + interval, count);
							List<Integer> sub = list.subList(start, end);
							new Thread(new MergeServer().new step11_doSql(sub))
									.start();
						}
						start = threadNum * interval;
						end = count;
						if (end > start) {
							List<Integer> sub = list.subList(start, end);
							new Thread(new MergeServer().new step11_doSql(sub))
									.start();
						}

						startTick = System.currentTimeMillis();
						while (true) {
							if (step1_subFinish < step1_subTotal) {
								int sec = (int) Math
										.max(
												1,
												(System.currentTimeMillis() - startTick) / 1000);
								int speed = Math.max(1, step1_subFinish / sec);
								int restTime = (step1_subTotal - step1_subFinish)
										/ speed;
								int totalTime = sec + restTime;
								show("finish:" + step1_subFinish + "/"
										+ step1_subTotal + ",speed:" + speed
										+ ",restTime:" + restTime
										+ "秒,totalTime:" + totalTime + "秒");
							} else {
								show("finish:" + step1_subFinish + "/"
										+ step1_subTotal);
								break;
							}
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
				}

				show("ownerMap size:" + ownerMap.size());
				{
					// 启动11条线程平摊所有记录进行多线程处理: step1_totalCount = 10 * interval +
					// rest;
					// 前10条各处理interval条记录，第11条线程处理rest条记录
					List<MergeStep1Model3> list = new ArrayList<MergeStep1Model3>();
					for (Entry<Integer, List<Object>> entry : ownerMap
							.entrySet()) {
						MergeStep1Model3 m = new MergeStep1Model3();
						m.setRoleId(entry.getKey());
						m.setObjList(entry.getValue());
						list.add(m);
					}
					int count = list.size();
					int threadNum = 10;
					int start, end;
					step1_subTotal = count;
					step1_subFinish = 0;
					if (count > 0) {
						int interval = Math.max(1, count / threadNum);
						for (int i = 0; i < threadNum; i++) {
							start = i * interval;
							if (start >= count) {
								break;
							}
							end = Math.min(start + interval, count);
							List<MergeStep1Model3> sub = list.subList(start,
									end);
							new Thread(new MergeServer().new step12_doSql(sub))
									.start();
						}
						start = threadNum * interval;
						end = count;
						if (end > start) {
							List<MergeStep1Model3> sub = list.subList(start,
									end);
							new Thread(new MergeServer().new step12_doSql(sub))
									.start();
						}

						startTick = System.currentTimeMillis();
						while (true) {
							if (step1_subFinish < step1_subTotal) {
								int sec = (int) Math
										.max(
												1,
												(System.currentTimeMillis() - startTick) / 1000);
								int speed = Math.max(1, step1_subFinish / sec);
								int restTime = (step1_subTotal - step1_subFinish)
										/ speed;
								int totalTime = sec + restTime;
								show("finish:" + step1_subFinish + "/"
										+ step1_subTotal + ",speed:" + speed
										+ ",restTime:" + restTime
										+ "秒,totalTime:" + totalTime + "秒");
							} else {
								show("finish:" + step1_subFinish + "/"
										+ step1_subTotal);
								break;
							}
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
				}

				show("step1 finished");
			}
		}
		if (MmochatMain.mergeServerStep == 2) {
			// 处理role表
			// 1. friends,enermys,students中的roleId增加30万
			boolean ret = step2();
			if (!ret) {
				return;
			}
			long startTick = System.currentTimeMillis();
			while (true) {
				if (step2_finishCount < step2_totalCount) {
					int sec = (int) Math.max(1,
							(System.currentTimeMillis() - startTick) / 1000);
					int speed = Math.max(1, step2_finishCount / sec);
					int restTime = (step2_totalCount - step2_finishCount)
							/ speed;
					int totalTime = sec + restTime;
					show("finish:" + step2_finishCount + "/" + step2_totalCount
							+ ",updateCount:" + step2_trueCount + ",speed:"
							+ speed + ",restTime:" + restTime + "秒,totalTime:"
							+ totalTime + "秒");
				} else {
					show("finish:" + step2_finishCount + "/" + step2_totalCount
							+ ",updateCount:" + step2_trueCount);
					break;
				}
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			show("step2 finished");

			if (!test) {
				// 处理gang表
				// 1. gang_members:roleId增加30万
				step3();
			}
		}
	}

	// step1：bigmoney_deal,元宝订单通过程序进行完结
	public void step1() {
		show("step1 start...");
		List<MmochatBigMoneyDeal> list = new ArrayList<MmochatBigMoneyDeal>();
		for (MmochatBigMoneyDeal deal : MmochatBigMoneyDealService.dealMap
				.values()) {
			try {
				if (deal != null) {
					deal.setState(MmochatBigMoneyDealState.交易结束);
					list.clear();
					list.add(deal);
					MmochatDao.batchUpdateAfterBigMoneyDealTimeOut(list);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		show("deal bigmoneyDeal ok!");

		show("start deal borrowLog...");
		// 合区出借处理
		// 1、查30天内的交易记录，如果交易记录中有借条，则查找借方身上有没有借来的物品或宠物，并尝试归还到包裹或仓库。
		borrowRoleIdMap.clear();
		int minId = MmochatDao.getDelDealLogMinId(new Timestamp(System
				.currentTimeMillis()
				- 30 * MmochatConstant.MS_PER_DAY));
		int maxId = MmochatDao.getDelDealLogMaxId();
		step1_totalCount = maxId - minId + 1;
		show("borrowLog table size:" + step1_totalCount);
		if (step1_totalCount > 0) {
			// 启动51条线程平摊所有记录进行多线程处理: step1_totalCount = 50 * interval + rest;
			// 前50条各处理interval条记录，第51条线程处理rest条记录
			int threadNum = 50;
			int start, end;
			int interval = Math.max(1, step1_totalCount / threadNum);
			for (int i = 0; i < threadNum; i++) {
				start = minId + i * interval;
				end = Math.min(start + interval, maxId + 1);
				new Thread(new MergeServer().new step1_doSql(start, end))
						.start();
			}
			start = minId + threadNum * interval;
			end = maxId + 1;
			if (end > start) {
				new Thread(new MergeServer().new step1_doSql(start, end))
						.start();
			}
		} else {
			show("step1 finished!");
		}
	}

	class step1_doSql implements Runnable {
		int start, end;

		// [start, end)
		step1_doSql(int s, int e) {
			start = s;
			end = e;
		}

		@Override
		public void run() {
			try {
				// 分100次执行
				if (end > start) {
					int times = 99;
					int interval = Math.max(1, (end - start) / times);
					// (end - start)=times*interval + rest;
					for (int i = 0; i < times; i++) {
						int s = start + i * interval;
						int e = Math.min(s + interval, end);
						// [s, e)
						if (e > s) {
							// TODO
							try {
								List<Integer> r1 = MmochatDao
										.queryBorrowedRole1DealLog(s, e);
								for (Integer r : r1) {
									try {
										borrowRoleIdMap.put(r, r);
									} catch (Exception e1) {
									}
								}
							} catch (Exception e1) {
								e1.printStackTrace();
							}
							try {
								List<Integer> r1 = MmochatDao
										.queryBorrowedRole2DealLog(s, e);
								for (Integer r : r1) {
									try {
										borrowRoleIdMap.put(r, r);
									} catch (Exception e1) {
									}
								}
							} catch (Exception e1) {
								e1.printStackTrace();
							}
							synchronized (lock) {
								step1_finishCount += e - s;
							}
						}
					}
					int s = start + times * interval;
					int e = end;
					// [s, e)
					if (e > s) {
						try {
							List<Integer> r1 = MmochatDao
									.queryBorrowedRole1DealLog(s, e);
							for (Integer r : r1) {
								try {
									borrowRoleIdMap.put(r, r);
								} catch (Exception e1) {
								}
							}
						} catch (Exception e1) {
							e1.printStackTrace();
						}
						try {
							List<Integer> r1 = MmochatDao
									.queryBorrowedRole2DealLog(s, e);
							for (Integer r : r1) {
								try {
									borrowRoleIdMap.put(r, r);
								} catch (Exception e1) {
								}
							}
						} catch (Exception e1) {
							e1.printStackTrace();
						}
						synchronized (lock) {
							step1_finishCount += e - s;
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	class step11_doSql implements Runnable {
		List<Integer> list = null;

		// [start, end)
		step11_doSql(List<Integer> l) {
			list = l;
		}

		@Override
		public void run() {
			try {
				if (list != null) {
					for (Integer roleId : list) {
						try {
							MergeStep1Model1 data = MmochatDao
									.getMergeStep1Model1(roleId);
							if (data != null) {
								// TODO:查找package, storage, pets,
								// stored_pets中的出借物品，并进行归还
								Gson gson = new Gson();

								// 包裹
								String str = data.getPackageStr();
								if (str != null) {
									Map<Integer, MmochatPackageGrid> myPackage = gson
											.fromJson(
													str,
													new TypeToken<Map<Integer, MmochatPackageGrid>>() {
													}.getType());
									if (myPackage != null) {
										for (MmochatPackageGrid grid : myPackage
												.values()) {
											MmochatCommonObject obj = grid
													.getObject();
											if (obj != null) {
												if (obj.getBorrowFromRoleId() != null) {
													// 借来的物品
													int targetRoleId = obj
															.getBorrowFromRoleId();
													// 从此人身上移除
													grid.setObject(null);

													// 归还给原主人
													obj
															.setBorrowFromRoleId(null);
													obj
															.setEquipUserRoleId(null);
													synchronized (ownerMap) {
														List<Object> objList = ownerMap
																.get(targetRoleId);
														if (objList == null) {
															objList = new ArrayList<Object>();
															ownerMap
																	.put(
																			targetRoleId,
																			objList);
														}
														objList.add(obj);
													}
												}
											}
										}

										String newStr = gson
												.toJson(
														myPackage,
														new TypeToken<Map<Integer, MmochatPackageGrid>>() {
														}.getType());
										if (newStr != null) {
											data.setPackageStr(newStr);
										}
									}

								}

								// 仓库
								str = data.getStorageStr();
								if (str != null) {
									Map<Integer, MmochatPackageGrid> myStorage = gson
											.fromJson(
													str,
													new TypeToken<Map<Integer, MmochatPackageGrid>>() {
													}.getType());
									if (myStorage != null) {
										for (MmochatPackageGrid grid : myStorage
												.values()) {
											MmochatCommonObject obj = grid
													.getObject();
											if (obj != null) {
												if (obj.getBorrowFromRoleId() != null) {
													// 借来的物品
													int targetRoleId = obj
															.getBorrowFromRoleId();
													// 从此人身上移除
													grid.setObject(null);

													// 归还给原主人
													obj
															.setBorrowFromRoleId(null);
													synchronized (ownerMap) {
														List<Object> objList = ownerMap
																.get(targetRoleId);
														if (objList == null) {
															objList = new ArrayList<Object>();
															ownerMap
																	.put(
																			targetRoleId,
																			objList);
														}
														objList.add(obj);
													}
												}
											}
										}
										String newStr = gson
												.toJson(
														myStorage,
														new TypeToken<Map<Integer, MmochatPackageGrid>>() {
														}.getType());
										if (newStr != null) {
											data.setStorageStr(newStr);
										}
									}
								}

								// 宠物
								str = data.getPets();
								if (str != null) {
									Map<Integer, MmochatPet> pets = gson
											.fromJson(
													str,
													new TypeToken<Map<Integer, MmochatPet>>() {
													}.getType());
									if (pets != null) {
										Map<Integer, MmochatPet> newPets = new ConcurrentHashMap<Integer, MmochatPet>();
										for (MmochatPet pet : pets.values()) {
											newPets.put(pet.getId(), pet);
										}
										pets = newPets;

										for (MmochatPet pet : pets.values()) {

											if (pet.getBorrowFromRoleId() != null) {
												// 借来的物品
												int targetRoleId = pet
														.getBorrowFromRoleId();
												// 从此人身上移除
												pets.remove(pet.getId());

												// 归还给原主人
												pet.setBorrowFromRoleId(null);
												pet
														.setState(MmochatPetStateType.休息);
												synchronized (ownerMap) {
													List<Object> objList = ownerMap
															.get(targetRoleId);
													if (objList == null) {
														objList = new ArrayList<Object>();
														ownerMap.put(
																targetRoleId,
																objList);
													}
													objList.add(pet);
												}
											}

										}
										String newStr = gson
												.toJson(
														pets,
														new TypeToken<Map<Integer, MmochatPet>>() {
														}.getType());
										if (newStr != null) {
											data.setPets(newStr);
										}
									}
								}

								// 钱庄宠物
								str = data.getStored_pets();
								if (str != null) {
									Map<Integer, MmochatPet> stored_pets = gson
											.fromJson(
													str,
													new TypeToken<Map<Integer, MmochatPet>>() {
													}.getType());
									if (stored_pets != null) {
										Map<Integer, MmochatPet> newPets = new ConcurrentHashMap<Integer, MmochatPet>();
										for (MmochatPet pet : stored_pets
												.values()) {
											newPets.put(pet.getId(), pet);
										}
										stored_pets = newPets;

										for (MmochatPet pet : stored_pets
												.values()) {

											if (pet.getBorrowFromRoleId() != null) {
												// 借来的物品
												int targetRoleId = pet
														.getBorrowFromRoleId();
												// 从此人身上移除
												stored_pets.remove(pet.getId());

												// 归还给原主人
												pet.setBorrowFromRoleId(null);
												synchronized (ownerMap) {
													List<Object> objList = ownerMap
															.get(targetRoleId);
													if (objList == null) {
														objList = new ArrayList<Object>();
														ownerMap.put(
																targetRoleId,
																objList);
													}
													objList.add(pet);
												}
											}

										}
										String newStr = gson
												.toJson(
														stored_pets,
														new TypeToken<Map<Integer, MmochatPet>>() {
														}.getType());
										if (newStr != null) {
											data.setStored_pets(newStr);
										}
									}
								}

								try {
									MmochatDao.updateMergeStep1Model1(data,
											roleId);
								} catch (Exception e) {
									System.out
											.println("roleId:"
													+ roleId
													+ ",MmochatDao.updateMergeStep1Model1(data, roleId)失败");
								}
							}
							synchronized (lock) {
								step1_subFinish++;
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
					}

				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	class step12_doSql implements Runnable {
		List<MergeStep1Model3> list = null;

		// [start, end)
		step12_doSql(List<MergeStep1Model3> l) {
			list = l;
		}

		@Override
		public void run() {
			try {
				if (list != null) {
					for (MergeStep1Model3 info : list) {
						try {
							// 归还物品
							MergeStep1Model4 data = MmochatDao
									.getMergeStep1Model4(info.getRoleId());
							if (data != null) {
								Gson gson = new Gson();

								Map<Integer, MmochatPackageGrid> myPackage = null;
								Map<Integer, MmochatPackageGrid> myStorage = null;
								Map<Integer, MmochatPet> stored_pets = null;
								for (Object obj : info.getObjList()) {
									if (obj instanceof MmochatCommonObject) {
										// 物品，先尝试归还到包裹;
										boolean success = false;
										if (myPackage == null) {
											myPackage = gson
													.fromJson(
															data
																	.getPackageStr(),
															new TypeToken<Map<Integer, MmochatPackageGrid>>() {
															}.getType());
										}
										for (MmochatPackageGrid grid : myPackage
												.values()) {
											if (grid != null
													&& !grid.hasObj()
													&& grid.getGridType() == MmochatPackageGridType.包裹物品格子
													&& grid.isCanUse()) {
												grid
														.setObject((MmochatCommonObject) obj);
												success = true;
												break;
											}
										}
										// 再尝试归还到钱庄
										if (!success) {
											if (myStorage == null) {
												myStorage = gson
														.fromJson(
																data
																		.getStorageStr(),
																new TypeToken<Map<Integer, MmochatPackageGrid>>() {
																}.getType());
											}
											for (MmochatPackageGrid grid : myStorage
													.values()) {
												if (grid != null
														&& !grid.hasObj()) {
													grid
															.setObject((MmochatCommonObject) obj);
													success = true;
													break;
												}
											}

											if (!success) {
												// 对方包裹、钱庄都满，无法归还成功；写文件
												String msg = "\n---------------------------\nroleId:"
														+ info.getRoleId()
														+ "\n";
												String gsonString = gson
														.toJson(
																obj,
																new TypeToken<MmochatCommonObject>() {
																}.getType());
												msg += gsonString
														+ "\n---------------------------";
												MmochatUtil.writeToFile(
														"mergeError", msg);
												System.out
														.println("一件物品归还失败，对方已满!");
											}
										}
									} else if (obj instanceof MmochatPet) {
										// 宠物，归还到钱庄
										if (stored_pets == null) {
											if (data.getStored_pets() == null
													|| data.getStored_pets()
															.length() == 0) {
												stored_pets = new ConcurrentHashMap<Integer, MmochatPet>();
											} else {
												stored_pets = gson
														.fromJson(
																data
																		.getStored_pets(),
																new TypeToken<Map<Integer, MmochatPet>>() {
																}.getType());
												Map<Integer, MmochatPet> newPetMap = new ConcurrentHashMap<Integer, MmochatPet>();
												for (MmochatPet pet : stored_pets
														.values()) {
													newPetMap.put(pet.getId(),
															pet);
												}
												stored_pets = newPetMap;
											}
										}
										MmochatPet pet = (MmochatPet) obj;
										stored_pets.put(pet.getId(), pet);
									}
								}

								// 更新字符串
								if (myPackage != null) {
									String newStr = gson
											.toJson(
													myPackage,
													new TypeToken<Map<Integer, MmochatPackageGrid>>() {
													}.getType());
									if (newStr != null) {
										data.setPackageStr(newStr);
									}
								}

								if (myStorage != null) {
									String newStr = gson
											.toJson(
													myStorage,
													new TypeToken<Map<Integer, MmochatPackageGrid>>() {
													}.getType());
									if (newStr != null) {
										data.setStorageStr(newStr);
									}
								}

								if (stored_pets != null) {
									String newStr = gson
											.toJson(
													stored_pets,
													new TypeToken<Map<Integer, MmochatPet>>() {
													}.getType());
									if (newStr != null) {
										data.setStored_pets(newStr);
									}
								}

								try {
									MmochatDao.updateMergeStep1Model4(data,
											info.getRoleId());
								} catch (Exception e) {
									System.out.println("roleId:"
											+ info.getRoleId()
											+ ",updateMergeStep1Model4失败");
								}
							}
							synchronized (lock) {
								step1_subFinish++;
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
					}

				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	// 处理role表
	// 1. friends,enermys,students中的roleId增加30万
	public boolean step2() {
		show("step2 start...");

		// 首先读取记录总条数
		int max = 0;
		try {
			max = MmochatDao.getRoleNum();
		} catch (Exception e) {
			e.printStackTrace();
			show("MmochatDao.getRoleNum() failed!");
			return false;
		}
		step2_totalCount = max + 1;
		show("role table size:" + step2_totalCount);

		// 启动51条线程平摊所有记录进行多线程处理: max = 50 * interval + rest;
		// 前50条各处理interval条记录，第51条线程处理rest条记录
		int threadNum = 50;
		int start, end;
		int interval = Math.max(1, max / threadNum);
		for (int i = 0; i < threadNum; i++) {
			start = i * interval;
			end = Math.min((i + 1) * interval, max + 1);
			new Thread(new MergeServer().new step2_doSql(start, end)).start();
		}
		start = threadNum * interval;
		end = max + 1;
		if (end > start) {
			new Thread(new MergeServer().new step2_doSql(start, end)).start();
		}
		return true;
	}

	class step2_doSql implements Runnable {
		int start, end;

		step2_doSql(int s, int e) {
			start = s;
			end = e;
		}

		@Override
		public void run() {
			try {
				// 分100次执行
				// 对于friends!=''或enermys!=''或students!=''的记录读取出来
				// friends,enermys,students中的roleId增加30万
				if (end > start) {
					int times = 99;
					int interval = Math.max(1, (end - start) / times);
					// (end - start)=times*interval + rest;
					for (int i = 0; i < times; i++) {
						int s = start + i * interval;
						int e = Math.min(s + interval, end);
						// [s, e)
						if (e > s) {
							// TODO
							List<MergeStep2Model1> data = null;
							try {
								data = MmochatDao.getMergeStep2Model1(s, e);
								if (!test) {
									for (MergeStep2Model1 m : data) {
										m.addRoleIdValue(roleIdAddition);
									}
								}
							} catch (Exception e1) {
								e1.printStackTrace();
								show("error:getMergeStep2Model1 failed,s=" + e
										+ ",e=" + e);
								synchronized (lock) {
									step2_finishCount += e - s;
									step2_trueCount += data.size();
								}
								continue;
							}
							if (data != null) {
								try {
									MmochatDao.updateMergeStep2Model1(data);
								} catch (Exception e1) {
									show("error:updateMergeStep2Model1 failed,s="
											+ e + ",e=" + e);
									synchronized (lock) {
										step2_finishCount += e - s;
										step2_trueCount += data.size();
									}
									continue;
								}
							}
							synchronized (lock) {
								step2_finishCount += e - s;
								step2_trueCount += data.size();
							}
						}
					}
					int s = start + times * interval;
					int e = end;
					// [s, e)
					if (e > s) {
						List<MergeStep2Model1> data = null;
						try {
							data = MmochatDao.getMergeStep2Model1(s, e);
							if (!test) {
								for (MergeStep2Model1 m : data) {
									m.addRoleIdValue(roleIdAddition);
								}
							}
						} catch (Exception e1) {
							show("error:getMergeStep2Model1 failed,s=" + e
									+ ",e=" + e);
							synchronized (lock) {
								step2_finishCount += e - s;
								step2_trueCount += data.size();
							}
							return;
						}
						if (data != null) {
							try {
								MmochatDao.updateMergeStep2Model1(data);
							} catch (Exception e1) {
								show("error:updateMergeStep2Model1 failed,s="
										+ e + ",e=" + e);
								synchronized (lock) {
									step2_finishCount += e - s;
									step2_trueCount += data.size();
								}
								return;
							}
						}
						synchronized (lock) {
							step2_finishCount += e - s;
							step2_trueCount += data.size();
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	// 处理gang表
	// 1. gang_members:roleId增加30万
	public void step3() {
		show("step3 start...");
		List<MmochatGangMember> list = new ArrayList<MmochatGangMember>();
		for (MmochatGang gang : MmochatGangService.gangs.values()) {
			list.clear();
			for (MmochatGangMember member : gang.getMembers().values()) {
				member.setRoleId(member.getRoleId() + roleIdAddition);
				list.add(member);
			}
			gang.getMembers().clear();
			for (MmochatGangMember member : list) {
				gang.getMembers().put(member.getRoleId(), member);
			}
			try {
				MmochatDao.updateGangMemberOfMergeServer(gang);
			} catch (Exception e) {
				show("error:updateGangMember failed!gang id=" + gang.getId());
			}
		}
		show("step3 finished!");
	}
}
